package com.kobako.controller;

import com.alibaba.fastjson.JSON;
import com.kobako.entity.EssayEntity;
import com.kobako.model.EssayDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by kobako on 2017/4/11.
 * Just a game
 */
@Controller
public class EssayController {
    @Autowired
    EssayDao essayDao;

    @ResponseBody
    @RequestMapping("/getAllEssays")
    public String getAllEssays() {
        List<EssayEntity> essays = essayDao.getAllTitles();
        return JSON.toJSONString(essays);
    }

    @ResponseBody
    @RequestMapping(value = "/getEssayById", method = RequestMethod.POST)
    public String getEssayById(@RequestParam(name = "id") int id) {
        EssayEntity essay = essayDao.getEssayById(id);
        return JSON.toJSONString(essay);
    }

    @ResponseBody
    @RequestMapping(value = "/getBlogById", method = RequestMethod.POST)
    public String getBlogById(@RequestParam(name = "id") int id) {
        EssayEntity essay = essayDao.getBlogById(id);
        return JSON.toJSONString(essay);
    }

    @ResponseBody
    @RequestMapping("/getAllBlogs")
    public String getAllBlogs() {
        List<EssayEntity> essays = essayDao.getAllBlogs();
        return JSON.toJSONString(essays);
    }
}
